﻿using System.Collections.Generic;
using System.Web;
using System.Web.Configuration;
using System.Net.Configuration;
using CommonGenius.Collections;
using AspNetInfo.Core;
using System;
using System.Data;
using System.Net.Mail;

namespace AspNetInfo.Detectors
{
    public class MailSettingsDetector : IDetector
    {
        public DataTable Detect(EnvironmentPermissions permissions)
        {
            var dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("SMTP");
            dt.Columns.Add("Mail");

            if (permissions.TrustLevel > AspNetHostingPermissionLevel.Medium)
            {

                var config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
                var mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
                var smtp = new SmtpClient();

                dt.Rows.Add("Host", mailSettings.Smtp.Network.Host, smtp.Host);
                dt.Rows.Add("Port", mailSettings.Smtp.Network.Port.ToString(), smtp.Port.ToString());
                dt.Rows.Add("DeliveryMethod", mailSettings.Smtp.DeliveryMethod.ToString(), smtp.DeliveryMethod.ToString());
                dt.Rows.Add("PickupDirectoryLocation", mailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation, smtp.PickupDirectoryLocation);
                dt.Rows.Add("EnableSsl", "N/A", smtp.EnableSsl.ToString());
                dt.Rows.Add("UseDefaultCredentials", mailSettings.Smtp.Network.DefaultCredentials.ToString(), smtp.UseDefaultCredentials.ToString());
                dt.Rows.Add("Timeout", "N/A", smtp.Timeout.ToString());
                dt.Rows.Add("Username", mailSettings.Smtp.Network.UserName, "N/A");
                dt.Rows.Add("Password", mailSettings.Smtp.Network.Password, "N/A");
                dt.Rows.Add("From", mailSettings.Smtp.From, "N/A");
            }
            return dt;
        }
        public string Name
        {
            get { return "Mail Settings"; }
        }


        public string Description
        {
            get { return String.Empty; }
        }

        public string TabName
        {
            get { return Tabs.Email; }
        }
    }
}
